import * as THREE from 'three';
// 获取模型底部四个边位置信息
export function getBoxHelperEdgeMidpointsOnScreen(object, camera) {
  const box = new THREE.Box3().setFromObject(object);
  // 获取Box的八个顶点坐标
  const vertices = [
    box.min.clone(),
    new THREE.Vector3(box.max.x, box.min.y, box.min.z),
    new THREE.Vector3(box.min.x, box.max.y, box.min.z),
    new THREE.Vector3(box.min.x, box.min.y, box.max.z),
    new THREE.Vector3(box.max.x, box.max.y, box.min.z),
    new THREE.Vector3(box.max.x, box.min.y, box.max.z),
    new THREE.Vector3(box.min.x, box.max.y, box.max.z),
    box.max.clone(),
  ];

  const edges = [
    [vertices[0], vertices[1]],
    // [vertices[0], vertices[2]],
    [vertices[0], vertices[3]],
    // [vertices[1], vertices[4]],
    [vertices[1], vertices[5]],
    // [vertices[2], vertices[4]],
    // [vertices[2], vertices[6]],
    [vertices[3], vertices[5]],
    // [vertices[3], vertices[6]],
    // [vertices[4], vertices[7]],
    // [vertices[5], vertices[7]],
    // [vertices[6], vertices[7]],
  ];

  const edgeMidpointsOnScreen = [];
  edges.forEach(edge => {
    const midpoint = edge[0]
      .clone()
      .add(edge[1])
      .multiplyScalar(0.5);
    midpoint.project(camera);
    const x = (midpoint.x * window.innerWidth) / 2 + window.innerWidth / 2;
    const y = -((midpoint.y * window.innerHeight) / 2) + window.innerHeight / 2;
    edgeMidpointsOnScreen.push({ x, y });
  });

  // 计算顶部面的中点位置
  const topFaceVertices = [vertices[2], vertices[6], vertices[4], vertices[7]];
  const topFaceMidpoint = new THREE.Vector3();
  topFaceVertices.forEach(vertex => {
    topFaceMidpoint.add(vertex);
  });
  topFaceMidpoint.divideScalar(topFaceVertices.length);
  topFaceMidpoint.project(camera);
  const topFaceX =
    (topFaceMidpoint.x * window.innerWidth) / 2 + window.innerWidth / 2;
  const topFaceY =
    -((topFaceMidpoint.y * window.innerHeight) / 2) + window.innerHeight / 2;
  edgeMidpointsOnScreen.push({ x: topFaceX, y: topFaceY });

  return edgeMidpointsOnScreen;
}

// 鼠标左键交点位置
export function setupMouseInteraction(
  camera,
  scene,
  callBackInput,
  callBackMove,
  callBackOutput,
) {
  var raycaster = new THREE.Raycaster();
  var mouse = new THREE.Vector2();
  var isMouseDown = false;

  document.addEventListener('mousedown', onDocumentMouseDown, false);
  document.addEventListener('mouseup', onDocumentMouseUp, false);
  document.addEventListener('mousemove', onDocumentMouseMove, false);

  function onDocumentMouseDown(event) {
    event.preventDefault();

    updateMousePosition(event);
    raycaster.setFromCamera(mouse, camera);

    var intersects = raycaster.intersectObjects(scene.children, true);

    if (intersects.length > 0) {
      var intersection = intersects[0];
      var intersectionPoint = intersection.point;
      // 将交点的位置转换为场景坐标
      // 这里可以执行你的自定义逻辑
      callBackInput && callBackInput(intersectionPoint);
      callBackInput && callBackInput(intersectionPoint);
    }

    isMouseDown = true;
  }

  function onDocumentMouseMove(event) {
    event.preventDefault();

    if (!isMouseDown) return;

    updateMousePosition(event);
    raycaster.setFromCamera(mouse, camera);

    var intersects = raycaster.intersectObjects(scene.children, true);

    if (intersects.length > 0) {
      var intersection = intersects[0];
      var intersectionPoint = intersection.point;
      console.log('鼠标移动中，交点位置：', intersectionPoint);
      callBackMove && callBackMove(intersectionPoint);
    }
  }

  function onDocumentMouseUp(event) {
    event.preventDefault();

    if (!isMouseDown) return;
    isMouseDown = false;

    document.removeEventListener('mousemove', onDocumentMouseMove, false);

    updateMousePosition(event);
    raycaster.setFromCamera(mouse, camera);

    var intersects = raycaster.intersectObjects(scene.children, true);
    if (intersects.length > 0) {
      var intersection = intersects[0];
      var intersectionPoint = intersection.point;
      // 将交点的位置转换为场景坐标
      // 这里可以执行你的自定义逻辑
      console.log('鼠标左键松开，交点位置：', intersectionPoint);
      callBackOutput && callBackOutput(intersectionPoint);
    }
  }

  function updateMousePosition(event) {
    mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
    mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  }
}
